package com.iflytek.aiui.demo.chat.handler;

import com.iflytek.aiui.demo.chat.model.SemanticResult;
import com.iflytek.aiui.demo.chat.ui.common.PermissionChecker;
import com.iflytek.aiui.demo.chat.ui.chat.ChatViewModel;
import com.iflytek.aiui.demo.chat.ui.chat.PlayerViewModel;
import com.zzhoujay.richtext.callback.OnUrlClickListener;
import com.zzhoujay.richtext.callback.OnUrlLongClickListener;

import org.json.JSONArray;

/**
 * 语义结果处理抽象类
 */

public abstract class IntentHandler implements OnUrlClickListener, OnUrlLongClickListener {
    public static final String NEWLINE = "<br/>";
    public static final String NEWLINE_NO_HTML = "\n";
    public static final String CONTROL_TIP = "你可以通过语音控制上一个，下一个哦";
    public static int controlTipReqCount = 0;

    protected ChatViewModel mMessageViewModel;
    protected PlayerViewModel mPlayer;
    protected PermissionChecker mPermissionChecker;

    public IntentHandler(ChatViewModel model, PlayerViewModel player, PermissionChecker checker){
        this.mPlayer = player;
        this.mMessageViewModel = model;
        this.mPermissionChecker = checker;
    }

    public abstract Answer getFormatContent(SemanticResult result);

    @Override
    public boolean urlClicked(String url) {
        return true;
    }

    @Override
    public boolean urlLongClick(String url) {
        return true;
    }

    //减少播放控制类指令提示次数
    public static boolean isNeedShowControlTip() {
        return controlTipReqCount++ % 5 == 0;
    }

    protected String optSlotValue(SemanticResult result, String slotKey) {
        JSONArray slots = result.semantic.optJSONArray("slots");
        for(int index = 0; index < slots.length(); index++) {
            String key = slots.optJSONObject(index).optString("name");

            if(key.equalsIgnoreCase(slotKey)) {
               return  slots.optJSONObject(index).optString("value");
            }
        }

        return null;
    }
}
